package com.example.Config;

import com.example.utils.JacksonObjectMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;

//SpringMVC的配置类
@Configuration
//直接使用SpringBoot框架创建的项目只需要实现WebMvcConfigurer接口不是继承WebMvcConfigurationSupport
public class MVCConfig implements WebMvcConfigurer {
    /**
     * 扩展MVC框架的消息转换器
     * @param converters
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        //将对象转换器设置到消息转换器中,底层使用Jackson转换
        mappingJackson2HttpMessageConverter.setObjectMapper(new JacksonObjectMapper());
        //配置使用对象转换器
        converters.add(0,mappingJackson2HttpMessageConverter);
    }
}
